package org.springblade.core.boot.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springblade.core.cloud.header.BladeHttpHeadersContextHolder;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@Configuration
public class FeignConfiguration implements RequestInterceptor {

	/**
	 * 设置header传递，将当前服务的header信息传递到下游服务
	 *
	 * @param template
	 */
	@Override
	public void apply(RequestTemplate template) {
//		ServletRequestAttributes attributes = (ServletRequestAttributes)
//			RequestContextHolder.getRequestAttributes();
//		if (attributes != null) {
//			HttpServletRequest request = attributes.getRequest();
//			Enumeration<String> headerNames = request.getHeaderNames();
//			if (headerNames != null) {
//				while (headerNames.hasMoreElements()) {
//					String name = headerNames.nextElement();
//					String values = request.getHeader(name);
//					// 跳过 content-length
//					if ("content-length".equals(name)) {
//						continue;
//					}
//					template.header(name, values);
//				}
//			}
//		}
//		HttpHeaders headers = BladeHttpHeadersContextHolder.get();
//		if (headers != null && !headers.isEmpty()) {
//			headers.forEach((key, values) -> {
//				values.forEach(value -> template.header(key, value));
//			});
//		}
	}
}
